package glub.hlzds.mijia;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class RSACoder {
    protected static byte[] getInitalVector() {
        return "0102030405060708".getBytes();
    }
    public static SecretKey generateSymmetricKey() throws Exception {
        try {
            KeyGenerator v0_1 = KeyGenerator.getInstance("AES");
            v0_1.init(new SecureRandom());
            return v0_1.generateKey();
        }
        catch(NoSuchAlgorithmException v0) {
            throw new Exception(((Throwable)v0));
        }
    }
    public static byte[] encrypt(byte[] arg5,byte[] key) throws Exception {
        try {
            Cipher v0 = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            SecretKeySpec keySpec = new SecretKeySpec(Base64.decodeBase64("7zDh2fTJYD+M6mhJ6bnorw==".getBytes()), "AES");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            v0.init(1, keySpec, new IvParameterSpec(getInitalVector()));
            return v0.doFinal(arg5);
        }
        catch(Exception v5) {
            throw new Exception("fail to encrypt by aescoder", ((Throwable)v5));
        }
    }

    public static PublicKey getCertificatePublicKey(byte[] arg2)  {
        if(arg2 != null) {
            try {
                return CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(arg2)).getPublicKey();
            }
            catch(CertificateException v2) {
            }
        }

        throw new IllegalStateException("public key bytes should not be empty");
    }


    //are3 1加密  2 解密
    public static byte[] cipher(byte[] arg1, Key arg2,int are3) throws Exception{
        Cipher v0 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        v0.init(are3, arg2);
        return v0.doFinal(arg1);
    }

    public static String x509Key = "-----BEGIN CERTIFICATE-----\nMIICDzCCAXigAwIBAgIEWBw0IzANBgkqhkiG9w0BAQUFADBMMQswCQYDVQQGEwJD\nTjEPMA0GA1UEChMGeGlhb21pMQ8wDQYDVQQLEwZ4aWFvbWkxGzAZBgNVBAMTEmFj\nY291bnQueGlhb21pLmNvbTAeFw0xNjExMDQwNzA5MjNaFw0xNzExMDQwNzA5MjNa\nMEwxCzAJBgNVBAYTAkNOMQ8wDQYDVQQKEwZ4aWFvbWkxDzANBgNVBAsTBnhpYW9t\naTEbMBkGA1UEAxMSYWNjb3VudC54aWFvbWkuY29tMIGfMA0GCSqGSIb3DQEBAQUA\nA4GNADCBiQKBgQCHcPEm9Wo8/LWHL8mohOV5YalTgZLzng+nWCEkIRP//6GohYlI\nh3dvGpueJvQ3Sany/3dLx0x6MQKA34NxRyoO37R/LgPZUfe6eWzHQeColBBHxTED\nbCqDh46Gv5vogjqHRl4+q2WGCmZOIfmPjNHQWG8sMIZyTqFCLc6gk9vSewIDAQAB\nMA0GCSqGSIb3DQEBBQUAA4GBAHaPnscaxSPh0N0Z5OgQ6PcWr5uYPLMweatYGZRH\nSFxwSqYXpqIowuRxmrBj+oE5rG5rzFCtNjCBoeMVy/7JXZr9Juaw9NCWaTaqrmIV\nP4nK/0kizCvkx3088OOCGextGeZUC9/PCbVUEcRvGLwSrvgqiC1KG4ufeIdQWBaJ\n8ZlG\n-----END CERTIFICATE-----\n";
    public static void main(String[] args) throws Exception {
//		String data = "h8V2e6:0bqc51:avtloQ#h8V2e6#W0FxJf#0Z75x4#yNx+NI:MQ==:T05FUExVUyBBNTAwMA==:MTU4NGJiYjM=:zwTOOJ:79Qamc:tlifxq:CzeZmH:NDYwMDExODUyMTA5NjA2:ODk4NjAxMTc4MzEwMTIwMTk1NDI=::NDYwMDE=::T25lUGx1cyA1";
//
//		byte[] ret = encrypt(data.getBytes());
//		String s = new String(Base64.encodeBase64(ret));
//
//		System.out.println(s);
//		arg2.getBytes("UTF-8")
        //加密
        byte[] bbb = "7zDh2fTJYD-M6mhJ6bnorw==".getBytes();
        byte[] aa = cipher(bbb,getCertificatePublicKey(x509Key),1);
        System.out.println(new String(Base64.encodeBase64(aa)));


        //解密
//		byte[] bbb = Base64.decodeBase64("NgY16VjQoSnBcFxHesbQhqVFdX1Zxqa_NrwQF85nOORhJqKUJZv1smugcwGvEDkdZYGmk8jwmRhsO0qMDMXNc354O8Hl44GUO9a2-lgO7fKVuVKK3Sqso5MR_qVWe510xvip64tsEK59A9TDRx41hqsBHG_nsEGOWNJBZsPm-j8=".getBytes());
//
//		byte[] aa = cipher(bbb,getCertificatePublicKey(x509Key),1);
//		System.out.println(new String(Base64.encodeBase64(aa)));


    }

    public static PublicKey getCertificatePublicKey(String arg2) throws Exception {
        return getCertificatePublicKey(arg2.getBytes("UTF-8"));
    }

}

